package com.nirvana.core;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Pattern;

import com.nirvana.util.NirvanaUtil;

public class Validator {
	
	private static final String REGEX_NUMBER = "[0-9]*";
	private static final String REGEX_ALPHA_NUMERIC = "[0-9a-zA-Z]*";
	
	public static Date MAX_DATE = null;
	private static final String MAX_DATE_STR = "9999-12-31";
	public static final String FOLDER_NAME_LOCK="lock";
	
	public static final String FORMAT_TIME_STAMP = "yyyy.MM.dd.hhmmss";

	static{
		try {
			MAX_DATE = NirvanaUtil.parse(MAX_DATE_STR, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean isNull(String text){
		boolean nullIndicator = false;
		if(text != null){
			if(!text.trim().equals(""))
				nullIndicator = false;
		}
		else
			nullIndicator = true;
		return nullIndicator;
	}
	
	/*public static boolean isValidEmailAddress(String emailId) {
		if (emailId == null)
			return false;
		try {
			InternetAddress emailAddr = new InternetAddress(emailId);
			emailAddr.validate();
		} catch (AddressException ex) {
			return false;
		}
		return true;
	}*/
	
	public static boolean isInteger(String value){
		if(isNullOrEmpty(value))
			return false;
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {}
		return false;
	}
	
	public static boolean isNumber(String value){
		if(isNullOrEmpty(value))
			return false;
		return Pattern.matches(REGEX_NUMBER, value);
	}
	
	public static boolean isAlphaNumeric(String value){
		if(isNullOrEmpty(value))
			return false;
		return Pattern.matches(REGEX_ALPHA_NUMERIC, value);
	}
	
	/**
	 * Method to Check if the Given value is NULL or EMPTY
	 * @param value
	 * @return
	 */
	public static boolean isNullOrEmpty(String value) {
		return (value == null) || value.trim().equals("");
	}
	
	public static boolean isNullOrEmpty(Object[] array) {
		return (array == null) || array.length==0;
	}

	public static boolean isNullOrEmpty(Collection list) {
		return (list == null) || list.isEmpty();
	}
	
	public static boolean areWithinOneMinute(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);

		// Make sure that c1 is earlier of the two
		if (c1.after(c2)) {
			c1.setTime(date2);
			c2.setTime(date1);
		}

		// If we can add a minute to c1 and it's now later than c2, then the
		// two dates were less than a minute apart.
		c1.add(Calendar.MINUTE,1);
        return c1.after(c2);
    }
	
	public static boolean isValidDate(String date, String format){
		if(date==null)
			return false;
		try {
			if (format == null)
				format = "yyyy-MM-dd";
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			simpleDateFormat.setLenient(false);
			simpleDateFormat.parse(date); //just for throwing the exception if it's an invalid date
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
